<template>
  <div class="content-box">
    <div class="page-content">
      <mt-search v-model="title" @keyup.enter.native="change"></mt-search>

      <div class="logo" v-if="showLogo">
        <img src="../../assets/images/logo.png" />
        <div class="tips">垃圾分类举手之劳，循环利用变废为宝</div>
      </div>
      <div v-else>
        <div class="list" v-if="newslist.length">
          <div class="item" v-for="(item,index) in newslist" :key="index" @click="popup(index)">
            <span>{{item.name}}</span><span>{{item.type | rubbish}}</span>
          </div>
        </div>

        <div class="nodata" v-else>
          <img src="../../assets/images/nodata.png" />
        </div>
      </div>
    </div>
    <mt-popup class="popup" v-model="popupVisible" position="middle">
      <div class="close" @click="close">X</div>
      <div class="solt" v-if="newslist[index]">
        <div>垃圾：<span>&emsp;&emsp;{{newslist[index].name}}</span></div>
        <div>种类：<span>&emsp;&emsp;{{newslist[index].type | rubbish}}</span></div>
        <div>描述：<span>&emsp;&emsp;{{newslist[index].explain}}</span></div>
        <div>提示：<span>&emsp;&emsp;{{newslist[index].tip}}</span></div>
        <div>其他：<span>&emsp;&emsp;{{newslist[index].contain}}</span></div>
      </div>
    </mt-popup>
  </div>
</template>

<script>
import { Popup, Toast } from "mint-ui";
export default {
  data() {
    return {
      index: 0,
      title: "",
      popupVisible: false,
      newslist: [],
      showLogo: true
    };
  },
  created() {},
  methods: {
    close() {
      this.popupVisible = false;
    },
    popup(index) {
      this.index = index;
      this.popupVisible = true;
    },
    change() {
      if (!this.title) {
        Toast({
          message: "请填写垃圾种类",
          position: "top",
          duration: 1000
        });
        return;
      }

      this.$axios
        .get(
          `http://api.tianapi.com/txapi/lajifenlei/index?key=ee4fe2fe33e09349c9174182acaf1743&word=` +
            this.title
        )
        .then(res => {
          this.showLogo = false;

          if (res.data.code == 200) {
            this.newslist = res.data.newslist;
          } else {
            this.newslist = [];
          }
        });
    },
    todetail() {
      this.$router.togo("/Home/Detail");
    }
  }, // 声明一个本地的过滤器
  filters: {
    rubbish: function(value) {
      let type;
      switch (value) {
        case 0:
          type = "可回收垃圾";
          break;
        case 1:
          type = "有害垃圾";
          break;
        case 2:
          type = "厨余垃圾";
          break;
        case 3:
          type = "其他垃圾";
          break;
      }
      return type;
    }
  },
  components: {
    Toast,
    Popup
  },
  computed: {}
};
</script>

<style scoped lang="less">
@import "~styles/index.less";
@import "~styles/variable.less";
.logo {
  margin: 2rem auto;
  img {
    width: 400px;
    height: 400px;
  }
  .tips {
    letter-spacing: 8px;
    font-size: 30px;
    font-weight: 700;
    margin-top: 30px;
  }
}
.nodata {
  margin: 150px auto;
  img {
    width: 400px;
    height: 100px;
  }
}
.list {
  overflow: visible;
  .item {
    border-bottom: 1px solid #e5e5e5;
    height: 80px;
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    align-items: center;
    margin: 20px 20px;
    font-size: 30px;
    // border-radius: 8px;
    // &:last-child {
    //   border: none;
    // }
  }
}
.popup {
  width: 80%;
  border-radius: 20px;
  .close {
    height: 60px;
    width: 60px;
    line-height: 60px;
    position: absolute;
    top: 10px;
    right: 20px;
    font-size: 36px;
    touch-action: none;
  }
  .solt {
    // padding: 40px 0;
    font-size: 30px;
    text-align: left;
    padding: 10px;
    div {
      display: flex;
      flex-direction: column;
      margin: 30px;
      color: #f75050;
      span {
        color: #333;
        margin-top: 20px;
        line-height: 40px;
      }
    }
  }
}
</style>
<style>
.mint-search {
  height: 110px !important;
}
.mint-searchbar-cancel {
  display: none;
}
.mint-search-list {
  display: none;
}
</style>
